package cube.net.frame;

import java.util.List;

/**
 * 帧构建器
 * 
 * @author abiao
 */
public abstract class FrameBuilder {

    /**
     * 帧选择器
     */
    private FrameSelector frameSelector;

    /**
     * FrameBuilder
     */
    public FrameBuilder(FrameSelector frameSelector) {
        this.frameSelector = frameSelector;
    }

    /**
     * 获取帧数据
     */
    public byte[] getBytes(Frame frame) {
        if (frame == null) {
            return null;
        }
        return seal(frame.getFrameBytes());
    }

    /**
     * 设置数据为帧属性
     */
    public Frame getFrame(byte[] pureData) {
        Frame frame = frameSelector.select(getIdentity(pureData));
        if (frame == null || !frame.isFrameBytesValid(pureData)) {
            return null;
        }
        return frame.setFrameBytes(pureData);
    }

    /**
     * 构建帧处理
     */
    public abstract Frame build(Frame frame);

    /**
     * 封帧方法
     */
    public abstract byte[] seal(byte[] data);

    /**
     * 解帧方法
     */
    public abstract byte[] unseal(byte[] data);

    /**
     * 分割数据
     */
    public abstract List<byte[]> split(byte[] data);

    /**
     * 从数据中获取帧标识
     */
    public abstract Identity getIdentity(byte[] data);
}
